package com.luomu.base.biz.impl;
import java.util.Date;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luomu.base.api.bo.UserLoginReqBO;
import com.luomu.base.api.service.SysLoginService;
import com.luomu.base.biz.mapper.SysSocialMapper;
import com.luomu.base.biz.mapper.SysUserMapper;
import com.luomu.base.biz.po.SysSocialPO;
import com.luomu.base.biz.po.SysUserPO;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthUser;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/sysLoginService")
public class SysLoginServiceImpl implements SysLoginService {

    @Autowired
    private SysSocialMapper sysSocialMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Long socialRegister(@RequestBody AuthUser authUserData){
        log.info("第三方登录开始:{}", JSON.toJSONString(authUserData));
        if(Objects.equals(authUserData.getSource(),"ALIPAY")){
            String uuid = authUserData.getRawUserInfo().getString("openId");
            authUserData.setUuid(uuid);
        }
        String authId = authUserData.getSource() + authUserData.getUuid();

        QueryWrapper<SysSocialPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysSocialPO::getAuthId,authId);
        List<SysSocialPO> sysSocialPOList=  sysSocialMapper.selectList(queryWrapper);

        if(CollectionUtils.isNotEmpty(sysSocialPOList)){
            // 登录
            return sysSocialPOList.get(0).getOpenId();
        }


        SysSocialPO sysSocialPO = new SysSocialPO();
//        sysSocialPO.setOpenId("");
//        sysSocialPO.setUserName("");
//        sysSocialPO.setEmail("");
        sysSocialPO.setAuthId(authId);
        sysSocialPO.setId(0L);
        sysSocialPO.setSource(authUserData.getSource());
        sysSocialPO.setNickName(authUserData.getNickname());
        sysSocialPO.setAvatar(authUserData.getAvatar());
        sysSocialPO.setAccessToken(authUserData.getToken().getAccessToken());
        sysSocialPO.setRefreshToken(authUserData.getToken().getRefreshToken());
        sysSocialPO.setExpireIn(authUserData.getToken().getExpireIn());
        sysSocialPO.setCreateTime(new Date());
        sysSocialPO.setUpdateTime(new Date());

        SysUserPO sysUserPO=new SysUserPO();
        sysUserPO.setNickName(sysSocialPO.getNickName());
        sysUserPO.setUserType("1");
        sysUserPO.setEmail(sysSocialPO.getEmail());
        sysUserPO.setAvatar(sysSocialPO.getAvatar());
        sysUserPO.setStatus("0");
        sysUserPO.setDelFlag("0");
        sysUserPO.setLoginDate(new Date());
        sysUserPO.setCreateTime(new Date());
        sysUserPO.setUpdateTime(new Date());
        sysUserPO.setRemark("第三方登录注册");

        sysUserMapper.insert(sysUserPO);
        sysSocialPO.setOpenId(sysUserPO.getOpenId());
        sysSocialMapper.insert(sysSocialPO);

        // 登录
        return sysSocialPO.getOpenId();
    }

    @Override
    public void login(@RequestBody UserLoginReqBO reqBO) {
        if(!StpUtil.isLogin()){
            StpUtil.login(reqBO.getOpenId());
        }
    }

    @Override
    public void logout() {

    }
}
